package com.hwn.datax.controller;

import com.hwn.datax.domain.RepositoryConfig;
import com.hwn.datax.service.RepositoryConfigService;
import com.hwn.datax.utils.AlertUtil;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * @author wnhuang
 * @date 2024/6/1 3:12
 */
@Controller
public class RepoConfigController {

    @FXML
    public TextArea schemaText;

    @FXML
    public TextArea tableText;

    @FXML
    public TextArea columnText;
    @FXML
    public TextField driverClassTemplateTextField;
    @FXML
    public TextField urlTemplateTextField;
    @FXML
    public TextField defalutPortTextField;

    @Autowired
    private RepositoryConfigService repoConfigService;

    @FXML
    public ComboBox taskGroupComboBox;

    public List<RepositoryConfig> list;

    @FXML
    public void onSave() {
        RepositoryConfig repositoryConfig = new RepositoryConfig();
        String selectedOption = (String) taskGroupComboBox.getValue();
        repositoryConfig.setDbName(selectedOption);
        repositoryConfig.setSchemaSql(schemaText.getText());
        repositoryConfig.setTableSql(tableText.getText());
        repositoryConfig.setColumnSql(columnText.getText());
        repositoryConfig.setDriverClass(driverClassTemplateTextField.getText());
        repositoryConfig.setUrlTemplate(urlTemplateTextField.getText());
        repositoryConfig.setDefaultPort(Integer.valueOf(defalutPortTextField.getText()));
        repoConfigService.save(repositoryConfig);
        AlertUtil.showSuccessAlert("保存成功");
    }

    private void showSuccessAlert(String msgContent) {
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setTitle("提示");
        alert.setHeaderText(null);
        alert.setContentText(msgContent);
        alert.showAndWait();
        // 初始化下拉框数据
        list = repoConfigService.list();
    }

    @FXML
    public void initialize() {

        // 初始化下拉框数据
        list = repoConfigService.list();

        taskGroupComboBox.getItems().addAll(list.stream().map(RepositoryConfig::getDbName).toList());
        // 设置默认值
        taskGroupComboBox.getSelectionModel().selectFirst();

        bindModel(list.get(0));

        taskGroupComboBox.setOnAction(event -> {
            String selectedOption = (String) taskGroupComboBox.getValue();
            for (RepositoryConfig repositoryConfig : list) {
                if (repositoryConfig.getDbName().equals(selectedOption)) {
                    bindModel(repositoryConfig);
                }
            }
        });
    }

    private void bindModel(RepositoryConfig repositoryConfig) {
        schemaText.setText(repositoryConfig.getSchemaSql());
        tableText.setText(repositoryConfig.getTableSql());
        columnText.setText(repositoryConfig.getColumnSql());
        driverClassTemplateTextField.setText(repositoryConfig.getDriverClass());
        urlTemplateTextField.setText(repositoryConfig.getUrlTemplate());
        defalutPortTextField.setText(String.valueOf(repositoryConfig.getDefaultPort()));
    }
}
